package com.code;

import java.sql.Timestamp;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailCuocGoi extends Activity{
	
	private ArrayList<String> detail;
	ArrayList<Integer> detailCuocgoi;
	private TextView tvPhone, tvDiaChi, tvMaSP, tvNgay, tvTienBan,tvSoLuong,tvNgayTruoc,tvSoNgay,tvNgayTiep,tvMaSo,tvThanhTien,tvDuNoCuoiKy,tvNgayBanGanNhat;
	private Button btTroVe, btCall;
	private String strPhoneNumber;
	ImageView imgWarning ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detailcuocgoi);
		
		//declare component
		tvMaSo =  (TextView)findViewById(R.id.tvMaSo);
		tvPhone = (TextView)findViewById(R.id.tvPhone);
		tvDiaChi = (TextView)findViewById(R.id.tvDiaChi);
		tvMaSP = (TextView)findViewById(R.id.tvMaSP);
		tvNgay = (TextView)findViewById(R.id.tvNgay);
		tvTienBan = (TextView)findViewById(R.id.tvTienBan);
		tvSoLuong = (TextView)findViewById(R.id.tvSoLuong);
		tvNgayTruoc= (TextView)findViewById(R.id.tvNgayTruoc);
		tvSoNgay = (TextView)findViewById(R.id.tvSoNgay);
		tvNgayTiep = (TextView)findViewById(R.id.tvNgayTiep);
		imgWarning = (ImageView)findViewById(R.id.imgWarning);
		tvThanhTien = (TextView)findViewById(R.id.tvThanhTien);
		tvDuNoCuoiKy = (TextView)findViewById(R.id.tvDuNoCuoiKy);
		btTroVe = (Button)findViewById(R.id.btTroVe);
		btCall = (Button)findViewById(R.id.btCall);
		tvNgayBanGanNhat = (TextView) findViewById(R.id.tvNgayBanGanNhat);
		
		detail = super.getIntent().getExtras().getStringArrayList("detail");
		detailCuocgoi = super.getIntent().getExtras().getIntegerArrayList("date_detail");
		
		tvMaSo.setText(detail.get(0));
		tvPhone.setText(detail.get(2));
		strPhoneNumber = detail.get(2).toString();
		this.setTitle(detail.get(5));
		tvDiaChi.setText(detail.get(4));
		tvMaSP.setText(detail.get(1));
		tvNgay.setText(detail.get(3).equals("") ? detail.get(3) : getHourtDate(detail.get(3)));
		tvTienBan.setText(detail.get(6));
		tvSoLuong.setText(detail.get(7));
		tvNgayTruoc.setText(detail.get(8).equals("") ? detail.get(8):Global.fomartDate(detail.get(8)));
		tvSoNgay.setText(detail.get(9));
		if(!detail.get(10).equals("") && checkDate(detail.get(10), detailCuocgoi))
		{
			tvNgayTiep.setText(detail.get(10).equals("") ? detail.get(10):Global.fomartDate(detail.get(10)));		
			imgWarning.setVisibility(View.GONE);
		}
		else
			tvNgayTiep.setText(detail.get(10).equals("") ? detail.get(10):Global.fomartDate(detail.get(10)));
		//du no cuoiky
		tvDuNoCuoiKy.setText(detail.get(11));
		//tong thanh tien
		tvThanhTien.setText(detail.get(12));
		tvNgayBanGanNhat.setText(detail.get(13).equals("") ? detail.get(13):Global.fomartDate(detail.get(13)));
		btCall.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+strPhoneNumber+""));
				startActivity(intent);
			}
		});
		
		btTroVe.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
	
	public static String getHourtDate(String date)
	{
		String convertDate ="";
		Timestamp tDate = Timestamp.valueOf(date);
		convertDate = String.valueOf(tDate.getHours()) +":"+ String.valueOf(tDate.getMinutes()) +":"+ String.valueOf(tDate.getSeconds());
		return convertDate;
	}
	
	public static Boolean checkDate(String dateNext, ArrayList<Integer> detailCuocgoi)
	{
		Timestamp tDateNext = Timestamp.valueOf(dateNext);
		if(tDateNext.getMonth()+1 > detailCuocgoi.get(1)){			
				return true;			
		}else if(tDateNext.getMonth()+1 < detailCuocgoi.get(1)){
			return false;
		}else
		{
			if(tDateNext.getDate() < detailCuocgoi.get(0))
				return false;
			else
				return true;
		}
	}
	
	public static String setMonth(int month)
	{
		String sMonth = String.valueOf(month);
		if(month < 9)
			sMonth = "0"+sMonth;
		return sMonth;	
	}
}
